package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminSetmealController")
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")
    public Result<String> save(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐:{}", setmealDTO);
        setmealService.save(setmealDTO);

        return Result.success();
    }

    /**
     * 套餐信息分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("套餐信息分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐信息分页查询:{}", setmealPageQueryDTO);
        PageResult pageResult = setmealService.page(setmealPageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 删除套餐信息-支持批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除套餐信息-支持批量删除")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result<String> delete(@RequestParam List<Long> ids){
        log.info("删除套餐信息-支持批量删除:{}", ids);
        setmealService.deleteBatch(ids);

        return Result.success();
    }

    /**
     * 起售/停售套餐信息
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("起售/停售套餐信息")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result<String> enableOrDisable(@PathVariable Integer status, Long id){
        log.info("起售/停售套餐信息:{}, {}", status, id);
        SetmealDTO setmealDTO = new SetmealDTO();
        setmealDTO.setStatus(status);
        setmealDTO.setId(id);

        setmealService.update(setmealDTO, false);

        return Result.success();
    }

    /**
     * 更新套餐及套餐菜品信息
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @ApiOperation("更新套餐及套餐菜品信息")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result<String> update(@RequestBody SetmealDTO setmealDTO){
        log.info("更新套餐及套餐菜品信息:{}", setmealDTO);
        setmealService.update(setmealDTO, true);

        return Result.success();
    }

    /**
     * 根据id获取套餐及套餐菜品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id获取套餐及套餐菜品信息")
    public Result<SetmealVO> getById(@PathVariable Long id){
        log.info("根据id获取套餐及套餐菜品信息:{}", id);
        SetmealVO setmealVO = setmealService.getById(id);

        return Result.success(setmealVO);
    }
}
